package com.ruoyi.project.bajiaostar.user;


import com.alibaba.excel.EasyExcel;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.framework.aop.UserRepeatSubmit;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.listener.entity.ImportGoodDto;
import com.ruoyi.framework.listener.entity.UpdateImportExcelDto;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.bajiaostar.tGood.domain.TGood;
import com.ruoyi.project.bajiaostar.tGood.service.ITGoodService;
import com.ruoyi.project.bajiaostar.tGoodDetail.domain.TGoodDetail;
import com.ruoyi.project.bajiaostar.tGoodDetail.service.ITGoodDetailService;
import com.ruoyi.project.bajiaostar.tGoodOrder.domain.TGoodOrder;
import com.ruoyi.project.bajiaostar.tUserAmount.domain.TUserAmount;
import com.ruoyi.project.bajiaostar.tUserAmount.service.ITUserAmountService;
import com.ruoyi.project.bajiaostar.tUserAmountDetail.domain.TUserAmountDetail;
import com.ruoyi.project.bajiaostar.tUserAmountDetail.service.ITUserAmountDetailService;
import com.ruoyi.project.bajiaostar.user.dto.FindUserAmountDetailDto;
import com.ruoyi.project.bajiaostar.user.dto.FindUserGoodOrderDto;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Date;
import java.util.List;

/**
 * 用户账户资金
 *
 */
@Slf4j
@RestController
@RequestMapping("/nt/amount")
public class WebAmountController extends BaseController {

    @Autowired
    ITUserAmountService userAmountService;
    @Autowired
    ITUserAmountDetailService userAmountDetailService;

    @ApiOperation("查询用户账户")
    @Log(title = "APP接口管理", businessType = BusinessType.OTHER)
    @PostMapping("/findUserAmount")
    @UserRepeatSubmit
    public AjaxResult findUserAmount () {
        try {
            Long appUserId = getAppUserId();
            TUserAmount amount = userAmountService.selectTUserAmountByUserId(appUserId);
            return AjaxResult.success(amount);
        } catch (Exception e) {
            log.error("查询用户账户失败 " + e.getMessage());
            throw new RuntimeException("查询用户账户失败");
        }
    }


    @ApiOperation("查询用户账户流水明细")
    @Log(title = "APP接口管理", businessType = BusinessType.OTHER)
    @PostMapping("/findUserAmountDetail")
    @UserRepeatSubmit
    public TableDataInfo findUserAmountDetail (@RequestBody FindUserAmountDetailDto dto) {
        try {
            Long appUserId = getAppUserId();
            startPage(dto.getPageNum(),dto.getPageSize());
            TUserAmountDetail detail=new TUserAmountDetail();
            detail.setUserId(appUserId);
            detail.setDetailName(dto.getDetailName());
            detail.setDetailType(dto.getType());
            detail.setGoodId(dto.getGoodId());
            if(!ObjectUtils.isEmpty(dto.getDateType())){
                if(dto.getDateType().equals(1)){
                    detail.setStartTime(DateUtils.getBeginDate(DateUtils.addDayTime(new Date(),-7)));
                }else if(dto.getDateType().equals(2)){
                    detail.setStartTime(DateUtils.getBeginDate(DateUtils.addDayTime(new Date(),-30)));
                }else if(dto.getDateType().equals(3)){
                    detail.setStartTime(DateUtils.getBeginDate(DateUtils.addDayTime(new Date(),-90)));
                }
                detail.setEndTime(DateUtils.getEndDate(new Date()));
            }
            List<TUserAmountDetail> list = userAmountDetailService.selectTUserAmountDetailList(detail);
            return getDataTable(list);
        } catch (Exception e) {
            log.error("查询用户账户流水明细失败 " + e.getMessage());
            throw new RuntimeException("查询用户账户流水明细失败");
        }
    }

    @ApiOperation("查询用户账户流水明细")
    @Log(title = "APP接口管理", businessType = BusinessType.OTHER)
    @PostMapping("/findAmountDetail")
    @UserRepeatSubmit
    public TableDataInfo findAmountDetail (@RequestBody FindUserAmountDetailDto dto) {
        try {
            startPage(dto.getPageNum(),dto.getPageSize());
            TUserAmountDetail detail=new TUserAmountDetail();
            detail.setUserId(dto.getUserId());
            detail.setDetailType(dto.getType());
            detail.setGoodId(dto.getGoodId());
            detail.setDetailName(dto.getDetailName());
            if(!ObjectUtils.isEmpty(dto.getDateType())){
                if(dto.getDateType().equals(1)){
                    detail.setStartTime(DateUtils.getBeginDate(DateUtils.addDayTime(new Date(),-7)));
                }else if(dto.getDateType().equals(2)){
                    detail.setStartTime(DateUtils.getBeginDate(DateUtils.addDayTime(new Date(),-30)));
                }else if(dto.getDateType().equals(3)){
                    detail.setStartTime(DateUtils.getBeginDate(DateUtils.addDayTime(new Date(),-90)));
                }
                detail.setEndTime(DateUtils.getEndDate(new Date()));
            }

            List<TUserAmountDetail> list = userAmountDetailService.selectTUserAmountDetailList(detail);
            return getDataTable(list);
        } catch (Exception e) {
            log.error("查询用户账户流水明细失败 " + e.getMessage());
            throw new RuntimeException("查询用户账户流水明细失败");
        }
    }


    @ApiOperation("全服平账")
    @Log(title = "APP接口管理", businessType = BusinessType.OTHER)
    @PostMapping("/executeAccountTask")
    @UserRepeatSubmit
    public String executeAccountTask () {
        try {

            return userAmountService.executeAccountTask();
        } catch (Exception e) {
            log.error("全服平账失败 " + e.getMessage());
            throw new RuntimeException("全服平账失败");
        }
    }
}
